<template>
  <a-modal
    :title="model && model.id > 0 ? '编辑押金' : '新增押金'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok')
      }
    "
    @cancel="
      () => {
        $emit('cancel')
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="id" v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item label="描述">
          <a-input v-decorator="['memo', { rules: [{ required: true, message: '请输入描述' }] }]" placeholder="请输入描述" />
        </a-form-item>
        <a-form-item label="押金归属">
          <a-select v-decorator="['depClsType', { initialValue: '1' }]" placeholder="请选择">
            <a-select-option value="1">供应商</a-select-option>
            <a-select-option value="2">经销商</a-select-option>
            <a-select-option value="3">运营商</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="押金类型">
          <a-select v-decorator="['depType', { initialValue: '1' }]" placeholder="请选择">
            <a-select-option value="1">收取</a-select-option>
            <a-select-option value="0">缴纳</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="账户">
          <a-select v-decorator="['accId']" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in this.option" :key="i.value">{{ i.accName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="金额">
          <a-input v-decorator="['amount', { rules: [{ required: true, message: '请输入金额' }] }]" placeholder="请输入金额" addonAfter="元" />
        </a-form-item>
        <a-form-item label="归属店">
          <a-select v-decorator="['storeId']" placeholder="请选择">
            <a-select-option :value="i.id" v-for="i in this.storeOption" :key="i.value">{{
              i.storeName
            }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="创建日期">
          <a-date-picker v-decorator="['date', { initialValue:this.toDays}]" style="width: 100%" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <!-- <a-form-item v-if="model && model.id > 0" label="状态">
          <a-select v-decorator="['status', { initialValue: '' }]">
            <a-select-option value="1">归还</a-select-option>
            <a-select-option value="0">待归还</a-select-option>
          </a-select>
        </a-form-item> -->
      </a-form>
    </a-spin>
  </a-modal>
</template>
  
  <script>
import pick from 'lodash.pick'
import { getStAccountListUsingGET } from '@/api/api/qiantaizhangwu.ts'
import { getStoreListUsingPOST } from '@/api/api/mendianguanli.ts'
import moment from 'moment'
// 表单字段
const fields = ['memo', 'depClsType', 'depType', 'accId', 'amount', 'date','storeId', 'status', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 },
      },
    }
    return {
      queryParam: {
        pageNo: 1,
        pageSize: 999,
        queryCondition: '',
        status: 1,
      },
      option: [],
      toDays: moment().format('YYYY-MM-DD'),
      storeOption: [],
      form: this.$form.createForm(this),
    }
  },
  created() {
    this.handleStore()
    this.handle()
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    handle() {
      getStAccountListUsingGET(this.queryParam)
        .then((res) => {
          this.option = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleStore() {
      getStoreListUsingPOST(this.queryParam)
        .then((res) => {
          this.storeOption = res.data.list
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
  },
}
</script>